package org.lc.stk.web.controller.user;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

import org.lc.stk.service.user.UserService;
import org.lc.stk.web.dto.user.BulkOperationRequest;
import org.lc.stk.web.dto.user.UserAdminCreateRequest;
import org.lc.stk.web.dto.user.UserAdminResponse;
import org.lc.stk.web.dto.user.UserQuery;
import org.lc.stk.web.response.ApiResponse;
import org.lc.stk.web.response.PageResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/user/users")
@Tag(name = "用户管理", description = "用户管理相关接口")
@SecurityRequirement(name = "bearerAuth")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    @Operation(summary = "创建用户")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiResponse<UserAdminResponse> createUser(@Valid @RequestBody UserAdminCreateRequest request) {
        return ApiResponse.success(UserAdminResponse.fromUser(userService.createUser(request)));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新用户")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiResponse<UserAdminResponse> updateUser(
            @Parameter(description = "用户ID") @PathVariable Integer id,
            @Valid @RequestBody UserAdminCreateRequest request) {
        return ApiResponse.success(UserAdminResponse.fromUser(userService.updateUser(id, request)));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiResponse<Void> deleteUser(@Parameter(description = "用户ID") @PathVariable Integer id) {
        userService.deleteUser(id);
        return ApiResponse.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取用户详情")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiResponse<UserAdminResponse> getUser(@Parameter(description = "用户ID") @PathVariable Integer id) {
        return ApiResponse.success(UserAdminResponse.fromUser(userService.getUser(id)));
    }

    @GetMapping
    @Operation(summary = "查询用户列表")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiResponse<PageResponse<UserAdminResponse>> searchUsers(
            @Parameter(description = "查询参数") UserQuery query,
            @PageableDefault Pageable pageable) {
        Page<UserAdminResponse> page = userService.searchUsers(query, pageable)
                .map(UserAdminResponse::fromUser);
        return ApiResponse.success(PageResponse.from(page));
    }

    @PostMapping("/batch")
    @Operation(summary = "批量操作用户")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiResponse<Void> bulkOperation(@Valid @RequestBody BulkOperationRequest request) {
        userService.bulkOperation(request);
        return ApiResponse.success();
    }
}